package com.cskaoyan.service;


import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.param.BaseParam;
import com.cskaoyan.module.pojo.MarketBrand;
import com.cskaoyan.module.pojo.MarketBrandExample;
import com.cskaoyan.module.regionvo.BrandListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.rmi.server.ExportException;
import java.util.Date;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService{

    @Autowired
    MarketBrandMapper marketBrandMapper;

    BaseRespVo baseRespVo =new BaseRespVo();

    @Override
    public BaseRespVo brandList(Integer page, Integer limit, Integer id, String name, String sort, String order) {
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (name != null) {
            criteria.andNameLike("%" +name+ "%");
        }
        example.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page,limit);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(example);
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(marketBrands);
        BrandListVo brandListVo = new BrandListVo((int) pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageSize(), pageInfo.getPageNum(), marketBrands);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(brandListVo);
        return baseRespVo;
    }


    @Override
    public BaseRespVo create(MarketBrand marketBrand) {
        BigDecimal floorPrice = marketBrand.getFloorPrice();
        String s = floorPrice.toString();
        marketBrand.setAddTime(new Date());
        marketBrand.setUpdateTime(new Date());
        marketBrand.setDeleted(false);
        int insert = marketBrandMapper.insert(marketBrand);
        Integer id = marketBrand.getId();
        MarketBrand marketBrand1 = marketBrandMapper.selectByPrimaryKey(id);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(marketBrand1);
        return baseRespVo;
    }

    @Override
    public BaseRespVo updateBrand(MarketBrand marketBrand) {
        int i = marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        if (i == 1) {
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
            baseRespVo.setData(marketBrand);
        }
        return baseRespVo;
    }

    @Override
    public BaseRespVo deleteBrand(MarketBrand marketBrand) {
        int i = marketBrandMapper.deleteByPrimaryKey(marketBrand.getId());
        if (i == 1) {
            baseRespVo.setErrmsg("成功");
            baseRespVo.setErrno(0);
        }
        return baseRespVo;
    }


}
